﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Template_AdminPage : System.Web.UI.UserControl
{
    class_database db;
    protected void Page_Load(object sender, EventArgs e)
    {
        string sessionUserType = Session["user_type"].ToString();
        if (sessionUserType != "0")
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        DisplayBVChoDuyet();
    }

    protected void DisplayBVChoDuyet()
    {
        DataTable dt = new DataTable();
        dt = GetBVChoDuyet();
        rptBaiVietChoDuyet.DataSource = dt;
        rptBaiVietChoDuyet.DataBind();
        foreach(RepeaterItem item in rptBaiVietChoDuyet.Items) 
        {
            SetHyperLink(item);
        }
    }

    protected DataTable GetBVChoDuyet()
    {
        DataTable dt = new DataTable();
        string sql = "SELECT tbl_baiphantich.id as IDBaiViet, tbl_mau.name as TenMau, tbl_loaibaiphantich.name as TenBai  FROM tbl_baiphantich, tbl_mau, tbl_loaibaiphantich" + 
            " WHERE permission = 2 AND tbl_mau.id = tbl_baiphantich.idmau AND tbl_loaibaiphantich.id = tbl_baiphantich.idloaibai ORDER BY idmau ";
        db = new class_database();
        dt = db.GetDataTable(sql);
        return dt;
    }

    protected void SetHyperLink(RepeaterItem item)
    {
        db = new class_database();
        HiddenField hdnIDBaiViet = item.FindControl("hdnIDBaiViet") as HiddenField;
        HyperLink hplTitleBaiViet = item.FindControl("hplTitleBaiViet") as HyperLink;
        string idBaiViet = hdnIDBaiViet.Value.ToString();
        string sql = "SELECT * FROM tbl_baiphantich WHERE id = " + idBaiViet;
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            string idLoaiBai = dt.Rows[0]["idloaibai"].ToString();
            string idMau = dt.Rows[0]["idmau"].ToString();
            if (idLoaiBai == "1")
            {
                hplTitleBaiViet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinChung&idMau=" + idMau;
            }
            else if (idLoaiBai == "2")
            {
                hplTitleBaiViet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinKyThuatChiTiet&idMau=" + idMau;
            }
            else if (idLoaiBai == "3")
            {
                hplTitleBaiViet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinFile&idMau=" + idMau;
            }
            else if (idLoaiBai == "4")
            {
                hplTitleBaiViet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinHamDiet&idMau=" + idMau;
            }
        }
    }

}